<template>
	<view class="tui-safe-area">
		<view class="tui-address">
			<block v-for="(item,index) in addressList" :key="index">
			
				<tui-list-cell padding="0" class="">
			
					<view class="tui-address-flex">
						    <radio-group class="radio-group" name="addressid" style="transform:scale(0.8)"
						    	@change="radioChange" v-if="istrues">
							<label class="tui-radio">
								<radio :value="item.id" color="#f28c28" :checked="index === current" />
							</label>
						</radio-group>
						<view class="tui-address-left">
			
							<view class="tui-address-main" @click="editAddr(item.id)">
								<view class="tui-address-name tui-ellipsis">{{item.addressee}}</view>
								<view class="tui-address-tel">{{item.mobile}}</view>
							</view>
							<view class="tui-address-detail" @click="editAddr(item.id)">
								<view class="tui-address-label" v-if="item.is_default===1">默认</view>
								<view class="tui-address-label">住宅</view>
								<text>{{item.address}}</text>
							</view>
						</view>
						<view class="tui-address-imgbox">
							<image class="tui-address-img" src="/static/images/mall/my/icon_addr_edit.png" />
						</view>
					</view>
			
				</tui-list-cell>
			</block>
		</view>
		<!--加载loadding-->
		<tui-loadmore v-if="loadding" :index="3" type="grey"></tui-loadmore>
		<view v-if="addressList==undefined">
			<tui-no-data :fixed="false" imgUrl="/static/images/nodata/error.png">暂无数据</tui-no-data>
		</view>
		<!-- 新增地址 -->
		<view class="tui-address-new">
			<tui-button shadow shape="circle" type="danger" height="88rpx" @click="addAddr">+ 新增收货地址</tui-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: [],
				isDoRefresh: false,
				loadding: false,
				current: 0,
				istrues:false,
			}
		},
		onLoad: function(options) {
			if(options){
				if(options.msg>0){
					if(options.msg==1){
						this.istrues=true;
					}
				}
				
				if(options.shopsMsg){
					this.shopsMsg=options.shopsMsg;
				}
			}
			
			
			this.tui.haslogin();
			if (this.tui.isLogin()) {
				this.init();
			}
		},
		onShow: function(e) {
			if (this.isDoRefresh == true) {
				this.isDoRefresh = false;
				this.init();
			}
		},
		methods: {
			setData(isDoRefresh) {
				this.isDoRefresh = true;
			},
			radioChange: function(e) {
				for (let i = 0; i < this.addressList.length; i++) {
			        
					if (this.addressList[i].id === e.detail.value) {
						this.current = i;
						setTimeout(()=>{
							let pages = getCurrentPages();
							let currPage = pages[pages.length - 1]; //当前页面
							let prevPage = pages[pages.length - 2]; //上一个页面
							prevPage.setData({
							  isDoRefresh:true,
							  isAddressId:this.addressList[i].id,
							  shopsMsg:this.shopsMsg,
							})
			
							 this.tui.setAddressId(this.addressList[i].id);
							//#ifdef MP-WEIXIN
							  prevPage.onLoad()
							//#endif
							uni.navigateBack();
						},1000)
						break;
					}
				}
			},
			init() {
				this.loadding = true;
				let token = this.tui.getToken();
				let datas = {
					token: token,
				};
				let address = this.tui.request('user', 'post', datas, 1000, 1, '', 'address');
				Promise.resolve(address).then((value) => {
					if (value.code == 200) {
						this.addressList = value.data;
						for (let i = 0; i < this.addressList.length; i++) {
							this.addressList[i].id=String(this.addressList[i].id)
							if(this.tui.getAddressId()==this.addressList[i].id){
								this.current=i;
							}
						}

					} else {
						this.tui.toast(value.message);
					}
					this.loadding = false;
				})
			},
			editAddr(index) {
				uni.navigateTo({
					url: "../editAddress/editAddress?adressid=" + index
				})
			},
			addAddr() {
				uni.navigateTo({
					url: "../editAddress/editAddress"
				})
			}
		}
	}
</script>

<style>
	.tui-address {
		width: 100%;
		padding-top: 20rpx;
		padding-bottom: 160rpx;
	}

	.tui-address-flex {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
    .tui-address-left{
    	margin-left: 20rpx;
    }
	.tui-address-main {
		width: 600rpx;
		height: 70rpx;
		display: flex;
		font-size: 30rpx;
		line-height: 86rpx;
		padding-left: 30rpx;
	}
	.radio-group {
	
		/* margin-right: 20rpx; */
		margin-left: 20rpx;
	}

	.tui-address-name {
		width: 120rpx;
		height: 60rpx;
	}

	.tui-address-tel {
		margin-left: 10rpx;
	}

	.tui-address-detail {
		font-size: 24rpx;
		word-break: break-all;
		padding-bottom: 25rpx;
		padding-left: 25rpx;
		padding-right: 120rpx;
	}

	.tui-address-label {
		padding: 5rpx 8rpx;
		flex-shrink: 0;
		background: #e41f19;
		border-radius: 6rpx;
		color: #fff;
		display: inline-flex;
		align-items: center;
		justify-content: center;
		font-size: 25rpx;
		line-height: 25rpx;
		transform: scale(0.8);
		transform-origin: center center;
		margin-right: 6rpx;
	}

	.tui-address-imgbox {
		width: 80rpx;
		height: 100rpx;
		position: absolute;
		display: flex;
		justify-content: center;
		align-items: center;
		right: 10rpx;
	}

	.tui-address-img {
		width: 36rpx;
		height: 36rpx;
	}

	.tui-address-new {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 999;
		padding: 20rpx 25rpx 30rpx;
		box-sizing: border-box;
		background: #fafafa;
	}

	.tui-safe-area {
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>